package com.example.moshudriver.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sgr.dykdriver.R;
import com.example.moshudriver.protocol.ORDER_RECORD;
import com.example.moshudriver.utils.StringUtils;

public class D11_OrderHistoryAdapter extends BaseAdapter {

	private Context mContext;
	public List<ORDER_RECORD> mList;
	private LayoutInflater mInflater;
	private int mCurrent = 0;
	private int mWidth;

	public D11_OrderHistoryAdapter(Context context, List<ORDER_RECORD> list) {
		this.mContext = context;
		this.mList = list;
		for (int i = 0; i < list.size(); i++) {
			mCurrent = i;
			if (list.get(i).active == 0) {
				if (i > 0) {
					mCurrent = i - 1;
				}
				break;
			}
		}
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		mWidth = dm.widthPixels;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.d2_order_history_item,
					null);
			holder.icon = (ImageView) convertView
					.findViewById(R.id.d11_order_history_item_icon);
			holder.text = (TextView) convertView
					.findViewById(R.id.d11_order_history_item_text);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ORDER_RECORD record = mList.get(position);

		holder.text
				.setText(StringUtils.getOrderStatusName(record.order_action));
		if (record.active == 1) {
			holder.icon.setImageResource(R.drawable.new_badge);
			holder.text.setTextColor(Color.parseColor("#333333"));
		} else {
			holder.icon.setImageResource(R.drawable.c5_circle);
			holder.text.setTextColor(Color.parseColor("#9b9b9b"));
		}

		if (position == mCurrent) {
			LayoutParams layoutParams_icon = (LayoutParams) holder.icon
					.getLayoutParams();
			layoutParams_icon.width = mWidth / 15;
			layoutParams_icon.height = mWidth / 15;
			holder.icon.setLayoutParams(layoutParams_icon);
			holder.text.setTextSize(20);
		} else {
			LayoutParams layoutParams_icon = (LayoutParams) holder.icon
					.getLayoutParams();
			layoutParams_icon.width = mWidth / 20;
			layoutParams_icon.height = mWidth / 20;
			holder.icon.setLayoutParams(layoutParams_icon);
			holder.text.setTextSize(18);
		}

		return convertView;
	}

	class ViewHolder {
		ImageView icon;
		TextView text;
	}
}
